<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      p {
        height: 400px;
        border: solid;
        font-size: 3rem;
      }
      #boxTop {
        width: 100px;
        height: 100px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 2rem;
        font-weight: bold;
        position: fixed;
        bottom: 20px;
        right: 20px;
        background: yellowgreen;
        color: white;
        cursor: pointer;
      }

      #boxTop:hover {
        background: rgb(101, 135, 33);
      }
    </style>
  </head>
  <body>
    <div id="boxTop" style="display: none">TOP</div>
    <div id="box">
      <p>
        p1

        <img
          src="./imgs/loading.gif"
          data-src="./imgs/1.jpg"
          height="200px"
          alt=""
        />
      </p>
      <p>p2</p>
      <p>p3</p>
      <p>p4</p>
      <p>
        p5
        <img
          src="./imgs/loading.gif"
          data-src="./imgs/2.jpg"
          height="200px"
          alt=""
        />
      </p>
      <p>
        p6

        <img src="./imgs/3.jpg" width="300px" alt="" />
      </p>
      <p>p7</p>
      <p>
        p8

        <img
          src="./imgs/loading.gif"
          data-src="./imgs/3.jpg"
          height="200px"
          alt=""
        />
      </p>
    </div>
    <script>
      // 节流 装饰器
      let flag = false;
      function fn() {
        flag = true;
        setTimeout(() => {
          console.log("hello");
          flag = false;
        }, 200);
      }
      window.onscroll = function () {
        if (flag === false) {
          fn();
        }
      };
    </script>
  </body>
</html>
